Ext.define('CS.data.proxy.Custom', {
	extend: 'Ext.data.proxy.Server',
	requires: ['Ext.util.MixedCollection', 'Ext.Ajax'],

	actionMethods: {
		create: 'POST',
		read: 'GET',
		update: 'POST',
		destroy: 'POST'
	},

	binary: false,

	doRequest: function(operation, callback, scope) {
		if (operation.action == 'destroy') {
			return null;
		} else {
			var params = operation.params || {};

			params.startIndex = operation.start;
			params.pageSize = operation.limit;

			var request = {
				url: this.url,
				jsonData: Ext.encode(params)
			};

			CS.apply(request, {
				binary: this.binary,
				headers: this.headers,
				timeout: this.timeout,
				scope: this,
				callback: this.createRequestCallback(request, operation, callback, scope),
				method: this.getMethod(request),
				disableCaching: false,
				async: operation.isAsync || true
			});

			Ext.Ajax.request(request);

			return request;
		}
	},

	getMethod: function(request) {
		return this.actionMethods[request.action];
	},

	createRequestCallback: function(request, operation, callback, scope) {
		var me = this;

		return function(options, success, response) {
			me.processResponse(success, operation, request, response, callback, scope);
		};
	}

});
